﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.IO;
using Microsoft.Xna.Framework;

namespace StreetlightGames.SpaceShooter.WaveMetaData
{
    public class WaveDataCollection : Collection<WaveData>
    {
        /// <summary>Gets or sets the FileName value</summary>
        [DataMember]
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        private string _fileName;
        
        /// <summary>
        /// Creates a new instance.
        /// </summary>
        public WaveDataCollection()
        {
        }

        public void SaveToFile(string fileName)
        {
            FileName = fileName;
            DataContractSerializer serializer = new DataContractSerializer(typeof(WaveDataCollection));
            FileStream stream = new FileStream(fileName, FileMode.Create);
            serializer.WriteObject(stream, this);
            stream.Close();
        }

        public static WaveDataCollection FromFile(string fileName)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(WaveDataCollection));
            FileStream stream = new FileStream(fileName, FileMode.Open);
            WaveDataCollection data = serializer.ReadObject(stream) as WaveDataCollection;
            stream.Close();
            data.FileName = fileName;
            return data;
        }


        internal static List<Wave> CreateWaves(Game game, WaveDataCollection data)
        {
            List<Wave> list = new List<Wave>();
            foreach (WaveData waveData in data)
            {
                list.Add(new Wave(game, waveData));
            }
            return list;
        }
    }
}
